package com.coldchain.order.api;


import com.alibaba.fastjson.JSONObject;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.enums.OrderBizTypeEnum;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.order.entity.BizDistributionOrderFromBean;
import com.coldchain.order.entity.Order;
import com.coldchain.order.entity.OrderSearch;
import com.coldchain.order.service.IBizDispatchingOrderService;
import com.coldchain.order.service.IOrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-09-11
 */
@RestController
@Api(description="订单模块")
@CheckUserToken
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private IOrderService orderService;

	@Autowired
	private IBizDispatchingOrderService bizDispatchingOrderService;


	/**
	 * 查询订单
	 * @param orderSearch
	 * @return
	 */
	@PostMapping("/dis/list")
	public Object  getOrderList( OrderSearch orderSearch){
		return bizDispatchingOrderService.getDispatchingOrderList(orderSearch);
	}

	/**
	 * 查询订单分页
	 * @param orderSearch
	 * @return
	 */
	@PostMapping("/dis/page")
	public Object  getOrderPage(OrderSearch orderSearch){
		 return bizDispatchingOrderService.getDispatchingOrderPage(orderSearch);
	}

	/**
	 * 创建订单
	 * @return
	 */
	@PostMapping("/create/distribution")
	public Object createOrder(String orders) {
		try {
			bizDispatchingOrderService.createOrder(JSONObject.parseObject(orders,BizDistributionOrderFromBean.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@GetMapping("/dis/simple/page")
	public Object selectSimpleOrderListForPage(OrderSearch search){
		return bizDispatchingOrderService.getDispatchingOrderList(search);
    }



	/**
	 * 获取订单祥情
	 * @param orderId
	 * @return
	 */
	@GetMapping("/getOrderDetail")
	public Object getOrderDetail(String orderId){
		Order order=orderService.getById(orderId);
		if(order==null){
			return  new ObjectRestResponse<>().error("订单不存在");
		}
		if(order.getBizType().equals(OrderBizTypeEnum.DISTRIBUTION.getValue())){
			return new ObjectRestResponse<>().data(bizDispatchingOrderService.getOrderDetail(orderId)).succeed("ok");
		}else {
			return  new ObjectRestResponse<>().succeed("ok");
		}
	}






}
